from smart_m3.Node import *
import uuid
import time
from RDFTransactionList import *

"""
written by fed :-)


    
"""

class M3():
    def __init__(self):
        print("\nSmart Space Access\n")

    def join(self,currNode = '_defName'):
        print "Joining..."
        self.nodename = str(uuid.uuid4()) + currNode
        self.theNode = ParticipantNode(self.nodename)
        self.theSmartSpace = self.theNode.discover()

        ans = self.theNode.join(self.theSmartSpace)

        print "*** Joined " + self.nodename + " with SmartSpace " + str(self.theSmartSpace) + " is " + str(ans)
    
    def createOntologicalStructures(self):
	
	ns_core="http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#"
	ns_semint="http://sofia.gotdns.com/ontologies/SemanticInteraction.owl#"

        p1 = self.theNode.CreateInsertTransaction(self.theSmartSpace)

	t = RDFTransactionList()
	t.add_Class(ns_semint + "InteractionPrimitive")
	p1.send( t.get() )  

	t = RDFTransactionList()
	t.add_Class(ns_semint + "IDType")
	p1.send( t.get() )  

	t = RDFTransactionList()
	t.add_Class(ns_semint + "InteractionPrimitiveType")
	p1.send( t.get() )  

        t = RDFTransactionList()	
        t.add_Class(ns_core + "Event")
        p1.send( t.get() )
        
        t = RDFTransactionList()
        t.add_Class(ns_core + "MediaPlayer")
        p1.send( t.get() )
        
	t = RDFTransactionList()        
        t.add_subClass(ns_core + "MediaPlayerEvent", ns_core + "Event")        
        p1.send( t.get() )

        t = RDFTransactionList()	
        t.add_Class(ns_core + "SmartObject")
        p1.send( t.get() )



        t = RDFTransactionList()	
        t.add_Class(ns_core + "Identification")
        p1.send( t.get() )

	self.theNode.CloseInsertTransaction(p1)

        

    def createInstances(self):
	
	ns="http://smartM3Lab/Ontology.owl#"
        t = RDFTransactionList()        

	u00 = ns + "RFID"
	t.setType(u00 , ns + "IdentificationType")
	u01 = ns + "Semacode"
	t.setType(u01, ns + "IdentificationType")


        u1 = ns + "Person_" + str(uuid.uuid4()) 	
        t.setType(u1, ns + "Person")
	t.add_literal(u1, ns + "HasName", "Luca")
        
	u2 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u2, ns + "IdentificationData")
	t.add_uri(u1, ns + "HasIdentificationData", u2) 
	t.add_literal(u2, ns + "HasValue", "111")
	t.add_uri(u2, ns + "HasIdentificationType", u00) 

	u3 = ns + "Environment_"+ str(uuid.uuid4())
	t.setType(u3, ns + "Environment")
	t.add_literal(u3, ns+"HasFriendlyName", "Room1")

	u4 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u4,ns + "IdentificationData")
	t.add_uri (u3, ns + "HasIdentificationData",u4)
	t.add_literal(u4, ns+"HasValue", "222")
	t.add_uri (u4, ns + "HasIdentificationType",u00)

	u5 = ns + "Environment_"+ str(uuid.uuid4())
	t.setType(u5, ns + "Environment")
	t.add_literal(u5, ns+"HasFriendlyName", "Room2")

	u6 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u6,ns + "IdentificationData")
	t.add_uri (u5, ns + "HasIdentificationData",u6)
	t.add_literal(u6, ns+"HasValue", "333")
	t.add_uri (u6, ns + "HasIdentificationType",u00)

	u7 = ns + "Item_"+ str(uuid.uuid4())
	t.setType(u7, ns + "Item")

	u8 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u8,ns + "IdentificationData")
	t.add_uri (u7, ns + "HasIdentificationData",u8)
	t.add_literal(u8, ns+"HasValue", "444")
	t.add_uri (u8, ns + "HasIdentificationType",u00)
	t.add_uri (u3, ns + "ContainsEntity", u7)

	u9 = ns + "Item_"+ str(uuid.uuid4())
	t.setType(u9, ns + "Item")

	u10 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u10,ns + "IdentificationData")
	t.add_uri (u9, ns + "HasIdentificationData",u10)
	t.add_literal(u10, ns+"HasValue", "555")
	t.add_uri (u10, ns + "HasIdentificationType",u01)
	u11 = ns + "IdentificationData_"+ str(uuid.uuid4())
	t.setType(u11,ns + "IdentificationData")
	t.add_uri (u9, ns + "HasIdentificationData",u11)
	t.add_literal(u11, ns+"HasValue", "666")
	t.add_uri (u11, ns + "HasIdentificationType",u00)
	t.add_uri (u5, ns + "ContainsEntity", u9)









	
	p1 = self.theNode.CreateInsertTransaction(self.theSmartSpace)
	p1.send( t.get() )
	
        self.theNode.CloseInsertTransaction(p1)     

    

            
	


if __name__ == '__main__':
    a=M3()
    a.join()
    a.createOntologicalStructures()
    
    

      

